﻿Imports EstoqueBLL
Public Class EntradaManualCAD
    Dim mc, m, pc, pv As New Decimal
    Dim op As Char
    Dim objproduto As New pecabll

#Region "Métodos"
#Region "Margem"
    Public Sub calculomargem(prev As Decimal, prec As Decimal)
        Try
            If (prec > 0 And prev > 0) Then
                m = (prev - prec) / prec
                TMargem.Text = FormatPercent(m)
            End If
        Catch ex As Exception
            Throw New Exception("Erro ao calcular margem.")
        End Try

    End Sub
#End Region

#Region "MargemContribuição"
    Public Sub calculomargemc(prev As Decimal, prec As Decimal)
        Try
            If (prec > 0 And prev > 0) Then
                mc = (prev - prec) / prev
                TMC.Text = FormatPercent(mc)
            End If
        Catch ex As Exception
            Throw New Exception("Erro ao calcular margem de contribuição.")
        End Try
    End Sub
#End Region

#Region "Preço Venda"
    Public Sub calculoprecovenda(marg As Decimal, margc As Decimal, prec As Decimal)
        Try
            If (prec > 0) Then
                If (marg > 0 And op = "M") Then
                    pv = (1 + marg) * pc
                    TPVenda.Text = FormatCurrency(pv)
                End If
                If (mc > 0 And op = "C") Then
                    Dim aux1 As Decimal
                    aux1 = 1 / (1 - mc)
                    pv = pc * aux1
                    TPVenda.Text = FormatCurrency(pv)
                End If
            End If
        Catch ex As Exception
            Throw New Exception("Erro ao calcular preço de venda.")
        End Try

    End Sub
#End Region

#Region "Carrega Propriedades"
    Public Sub carregapropriedades()
        If (Not IsNothing(objproduto)) Then
            TDescricao.Text = objproduto.pro_descricao
            TEstTotal.Text = objproduto.pec_qtde
            TPCusto.Text = FormatCurrency(objproduto.pro_precocusto)
            TPVenda.Text = FormatCurrency(objproduto.pro_precovenda)
            TMargem.Text = FormatPercent(objproduto.pec_margem / 100)
            TMC.Text = FormatPercent(objproduto.pec_mcontrib / 100)
            If (objproduto.pec_margem > 0) Then op = "M"
            If (objproduto.pec_mcontrib > 0) Then op = "C"
            TPCusto_LostFocus(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "Limpar"
    Public Sub limpar()
        op = ""
        mc = Nothing
        m = Nothing
        pv = Nothing
        pc = Nothing
        objproduto = Nothing
        tqtde.Clear()
        TEstTotal.Clear()
        TPCusto.Clear()
        TPVenda.Clear()
        TMC.Clear()
        TMargem.Clear()
        TDescricao.Clear()
        cboperacao.SelectedIndex = 0
        cboperacao_SelectedValueChanged(Nothing, Nothing)
        cboperacao.Refresh()
        cboperacao.Enabled = False
        gitens.Enabled = False
    End Sub
#End Region

#End Region

#Region "Eventos"
#Region "Busca Produto_Click"
    Private Sub BBuscaProduto_Click(sender As System.Object, e As System.EventArgs) Handles BBuscaProduto.Click
        Try
            Dim objpro As New PecaCON
            objpro.tipo = "ret"
            objpro.ShowDialog()
            If (Not IsNothing(objpro.objprod)) Then
                objproduto = objpro.objprod
                carregapropriedades()
                cboperacao.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
        
    End Sub
#End Region

#Region "cboperacao_selectedvalue"
    Private Sub cboperacao_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles cboperacao.SelectedValueChanged
        If (cboperacao.Text = "Entrada" Or cboperacao.Text = "Saída") Then
            tqtde.Enabled = True
            TPCusto.Enabled = False
            TPVenda.Enabled = False
            TMargem.Enabled = False
            TMC.Enabled = False
            gitens.Enabled = True
        ElseIf (cboperacao.Text = "Correção Custo") Then
            tqtde.Enabled = False
            TPCusto.Enabled = True
            TPVenda.Enabled = True
            TMargem.Enabled = True
            TMC.Enabled = True
            gitens.Enabled = True
        End If

    End Sub
#End Region

#Region "TPCusto_LostFocus"
    Private Sub TPCusto_LostFocus(sender As Object, e As System.EventArgs) Handles TPCusto.LostFocus
        Try
            Dim r, r1, r2, r3 As New Boolean
            r = Decimal.TryParse(TPCusto.Text.ToString.Replace("R$", ""), pc)
            r1 = Decimal.TryParse(TMargem.Text.ToString.Replace("%", ""), m)
            r2 = Decimal.TryParse(TMC.Text.ToString.Replace("%", ""), mc)
            r3 = Decimal.TryParse(TPVenda.Text.ToString.Replace("R$", ""), pv)
            TPCusto.Text = FormatCurrency(pc)
            If (True = r) Then
                m = m / 100
                mc = mc / 100
                'colocar metodos de calculos
                calculoprecovenda(m, mc, pc)
                calculomargem(pv, pc)
                calculomargemc(pv, pc)

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

    End Sub
#End Region

#Region "_Keypress"
    Private Sub TPCusto_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TPVenda.KeyPress, TPCusto.KeyPress, TMC.KeyPress, TMargem.KeyPress
        If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
        Dim obj As New TextBox
        obj = sender
        If (sender.Text.Contains(",") = False And e.KeyChar = ",") Then e.Handled = False

    End Sub

    Private Sub tqtde_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tqtde.KeyPress, TEstTotal.KeyPress
        If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub EntradaManualCAD_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
#End Region

#Region "Load"

    Private Sub EntradaManualCAD_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.cboperacao.SelectedIndex = 0
    End Sub
#End Region

#Region "TSBGravar_Click'"
    Private Sub TSBGravar_Click(sender As System.Object, e As System.EventArgs) Handles TSBGravar.Click
        Try
            Dim r, r1, r2, r3 As New Boolean
            Dim qtdd As Integer
            r = Decimal.TryParse(TPCusto.Text.ToString.Replace("R$", ""), pc)
            r1 = Decimal.TryParse(TMargem.Text.ToString.Replace("%", ""), m)
            r2 = Decimal.TryParse(TMC.Text.ToString.Replace("%", ""), mc)
            r3 = Decimal.TryParse(TPVenda.Text.ToString.Replace("R$", ""), pv)
            If (cboperacao.Text = "Correção Custo") Then qtdd = Val(TEstTotal.Text)
            If (cboperacao.Text = "Entrada") Then qtdd = Val(tqtde.Text) + Val(TEstTotal.Text)
            If (cboperacao.Text = "Saída") Then
                If (Val(TEstTotal.Text) - Val(tqtde.Text) < 0) Then
                    Throw New Exception("O estoque não pode ser menor que 0!")
                Else
                    qtdd = Val(TEstTotal.Text) - Val(tqtde.Text)
                End If
            End If
            If (objproduto.entradamanual(cboperacao.Text, Val(qtdd), pv, pc, mc, m, objusuarios.Codigo)) Then
                MessageBox.Show("Gravado com sucesso!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                limpar()
            Else
                Throw New Exception("Erro ao atualizar dados")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
#End Region

#Region "TPMargem_LostFocus"
    Private Sub TMargem_LostFocus(sender As Object, e As System.EventArgs) Handles TMargem.LostFocus
        'TMargem.Text = FormatPercent(TMargem.Text)
        op = "M"
        TPCusto_LostFocus(Nothing, Nothing)
    End Sub
#End Region

#Region "TPMargemC_LostFocus"
    Private Sub TMC_LostFocus(sender As Object, e As System.EventArgs) Handles TMC.LostFocus
        'TMC.Text = FormatPercent(TMC.Text)
        op = "C"
        Dim r2 As Boolean
        Dim marg As Decimal
        r2 = Decimal.TryParse(TMC.Text.ToString.Replace("%", ""), marg)
        If (Not marg / 100 >= 1) Then
            TPCusto_LostFocus(Nothing, Nothing)
        Else
            MessageBox.Show("A margem de contribuição tem que variar de 0 - 100%!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            TMC.Text = ""
            TMC.Focus()
        End If
    End Sub
#End Region

#Region "TPVenda_LostFocus"
    Private Sub TPVenda_LostFocus(sender As Object, e As System.EventArgs) Handles TPVenda.LostFocus
        TPVenda.Text = FormatCurrency(TPVenda.Text)
        Try
            Dim r, r1, r2, r3 As New Boolean
            r = Decimal.TryParse(TPCusto.Text.Replace("R$", ""), pc)
            r1 = Decimal.TryParse(TMargem.Text.Replace("%", ""), m)
            r2 = Decimal.TryParse(TMC.Text.Replace("%", ""), mc)
            r3 = Decimal.TryParse(TPVenda.Text.Replace("R$", ""), pv)
            If (True = r) Then
                m = m / 100
                mc = mc / 100
                'colocar metodos de calculos
                calculomargem(pv, pc)
                calculomargemc(pv, pc)
                calculoprecovenda(m, mc, pc)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        TMC.Text = FormatPercent(mc)
        TMargem.Text = FormatPercent(m)
        TPVenda.Text = FormatCurrency(pv)
    End Sub
#End Region

#Region "TSBVoltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region


#End Region
End Class